# coding= utf-8
import pymongo

class Category:
    def __init__(self):
        self.config = []

        # 连接MongoDB
        self.myclient = pymongo.MongoClient("mongodb://localhost:27017/")
        mydb = self.myclient["xp_analyse"]
        self.my_product = mydb["wt_order_product"]

    def update_root_category(self):
        all_product = self.my_product.find({"root_category": {"$exists": False}})
        for i in all_product:
            where = {"_id": i['_id']}
            if not i['categories_tree'] == '':
                root_cate_arr = i['categories_tree'].split(' > ')
                root_cate = root_cate_arr[0]
            else:
                root_cate = ''
            self.my_product.update_one(where, {"$set": {"root_cate": root_cate}})
            print(i['asin'] + '已更新rootCategory：' + root_cate)

category = Category()
category.update_root_category()
